# 使用ADO.NET创建文章表的PowerShell脚本
param(
    [string]$ConnectionString = "Host=localhost;Database=GroupSixDb;Username=postgres;Password=123456"
)

Write-Host "开始创建文章表..." -ForegroundColor Green

# 添加Npgsql程序集
Add-Type -Path "C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.0\System.Data.dll"
Add-Type -Path "C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.0\System.Data.Common.dll"

try {
    # 创建连接
    $connection = New-Object System.Data.Odbc.OdbcConnection
    $connection.ConnectionString = "Driver={PostgreSQL UNICODE};Server=localhost;Database=GroupSixDb;Uid=postgres;Pwd=123456;"
    
    $connection.Open()
    Write-Host "数据库连接成功！" -ForegroundColor Green
    
    # 创建命令对象
    $command = $connection.CreateCommand()
    
    # 创建文章分类表
    Write-Host "创建文章分类表..." -ForegroundColor Yellow
    $command.CommandText = @"
CREATE TABLE IF NOT EXISTS gs_article_categories (
    "Id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    "Name" character varying(100) NOT NULL,
    "Description" character varying(500) DEFAULT '',
    "ParentId" uuid,
    "Level" integer NOT NULL DEFAULT 1,
    "SortOrder" integer NOT NULL DEFAULT 0,
    "Code" text,
    "IsActive" boolean NOT NULL DEFAULT true,
    "IsDeleted" boolean NOT NULL DEFAULT false,
    "CreatedAt" timestamp with time zone NOT NULL DEFAULT NOW(),
    "UpdatedAt" timestamp with time zone NOT NULL DEFAULT NOW(),
    "CreatedBy" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',
    "UpdatedBy" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',
    "DisplayOrder" integer NOT NULL DEFAULT 0,
    "Remarks" text
);
"@
    $command.ExecuteNonQuery()
    
    # 创建文章表
    Write-Host "创建文章表..." -ForegroundColor Yellow
    $command.CommandText = @"
CREATE TABLE IF NOT EXISTS gs_articles (
    "Id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    "Title" character varying(200) NOT NULL,
    "Content" text NOT NULL,
    "Summary" character varying(500) DEFAULT '',
    "CategoryId" uuid NOT NULL,
    "CategoryName" character varying(100) DEFAULT '',
    "Tags" character varying(500) DEFAULT '',
    "Status" character varying(20) NOT NULL DEFAULT 'draft',
    "AuthorId" uuid NOT NULL,
    "AuthorName" character varying(50) DEFAULT '',
    "CoverImage" character varying(500) DEFAULT '',
    "ViewCount" integer NOT NULL DEFAULT 0,
    "LikeCount" integer NOT NULL DEFAULT 0,
    "CommentCount" integer NOT NULL DEFAULT 0,
    "PublishedAt" timestamp with time zone,
    "SeoTitle" character varying(200) DEFAULT '',
    "SeoKeywords" character varying(500) DEFAULT '',
    "SeoDescription" character varying(1000) DEFAULT '',
    "IsTop" boolean NOT NULL DEFAULT false,
    "IsRecommend" boolean NOT NULL DEFAULT false,
    "SortWeight" integer NOT NULL DEFAULT 0,
    "Code" text,
    "IsActive" boolean NOT NULL DEFAULT true,
    "IsDeleted" boolean NOT NULL DEFAULT false,
    "CreatedAt" timestamp with time zone NOT NULL DEFAULT NOW(),
    "UpdatedAt" timestamp with time zone NOT NULL DEFAULT NOW(),
    "CreatedBy" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',
    "UpdatedBy" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',
    "DisplayOrder" integer NOT NULL DEFAULT 0,
    "Remarks" text
);
"@
    $command.ExecuteNonQuery()
    
    # 创建索引
    Write-Host "创建索引..." -ForegroundColor Yellow
    $indexes = @(
        "CREATE INDEX IF NOT EXISTS `"IX_gs_article_categories_ParentId`" ON gs_article_categories (`"ParentId`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_article_categories_Level`" ON gs_article_categories (`"Level`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_article_categories_SortOrder`" ON gs_article_categories (`"SortOrder`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_CategoryId`" ON gs_articles (`"CategoryId`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_AuthorId`" ON gs_articles (`"AuthorId`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_Status`" ON gs_articles (`"Status`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_PublishedAt`" ON gs_articles (`"PublishedAt`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_IsTop`" ON gs_articles (`"IsTop`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_IsRecommend`" ON gs_articles (`"IsRecommend`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_SortWeight`" ON gs_articles (`"SortWeight`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_CreatedAt`" ON gs_articles (`"CreatedAt`");"
    )
    
    foreach ($index in $indexes) {
        $command.CommandText = $index
        $command.ExecuteNonQuery()
    }
    
    # 插入测试数据
    Write-Host "插入测试数据..." -ForegroundColor Yellow
    
    # 插入文章分类
    $command.CommandText = @"
INSERT INTO gs_article_categories ("Id", "Name", "Description", "Level", "SortOrder", "CreatedAt", "UpdatedAt", "CreatedBy", "UpdatedBy") VALUES
('11111111-1111-1111-1111-111111111111', '技术文章', '技术相关的文章分类', 1, 1, NOW(), NOW(), '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('22222222-2222-2222-2222-222222222222', '新闻资讯', '新闻资讯类文章', 1, 2, NOW(), NOW(), '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('33333333-3333-3333-3333-333333333333', '教程指南', '教程和指南类文章', 1, 3, NOW(), NOW(), '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000')
ON CONFLICT ("Id") DO NOTHING;
"@
    $command.ExecuteNonQuery()
    
    # 插入文章
    $command.CommandText = @"
INSERT INTO gs_articles ("Id", "Title", "Content", "Summary", "CategoryId", "CategoryName", "Tags", "Status", "AuthorId", "AuthorName", "ViewCount", "LikeCount", "CommentCount", "PublishedAt", "IsTop", "IsRecommend", "SortWeight", "CreatedAt", "UpdatedAt", "CreatedBy", "UpdatedBy") VALUES
('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'ASP.NET Core 开发指南', '这是一篇关于ASP.NET Core开发的详细指南...', 'ASP.NET Core开发入门指南', '11111111-1111-1111-1111-111111111111', '技术文章', 'ASP.NET Core,C#,Web开发', 'published', '00000000-0000-0000-0000-000000000000', '管理员', 150, 25, 8, NOW() - INTERVAL '5 days', true, true, 100, NOW() - INTERVAL '10 days', NOW() - INTERVAL '5 days', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'Vue.js 前端开发实践', 'Vue.js是现代前端开发的重要框架...', 'Vue.js前端开发最佳实践', '11111111-1111-1111-1111-111111111111', '技术文章', 'Vue.js,JavaScript,前端', 'published', '00000000-0000-0000-0000-000000000000', '管理员', 89, 15, 3, NOW() - INTERVAL '3 days', false, true, 80, NOW() - INTERVAL '8 days', NOW() - INTERVAL '3 days', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('cccccccc-cccc-cccc-cccc-cccccccccccc', '数据库设计原则', '良好的数据库设计是系统成功的关键...', '数据库设计的基本原则和最佳实践', '33333333-3333-3333-3333-333333333333', '教程指南', '数据库,设计,SQL', 'published', '00000000-0000-0000-0000-000000000000', '管理员', 67, 12, 5, NOW() - INTERVAL '1 day', false, false, 60, NOW() - INTERVAL '6 days', NOW() - INTERVAL '1 day', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('dddddddd-dddd-dddd-dddd-dddddddddddd', '系统架构设计', '系统架构设计需要考虑多个方面...', '系统架构设计的方法和技巧', '33333333-3333-3333-3333-333333333333', '教程指南', '架构,设计,系统', 'draft', '00000000-0000-0000-0000-000000000000', '管理员', 0, 0, 0, NULL, false, false, 0, NOW(), NOW(), '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee', '项目开发新闻', '最新的项目开发动态和进展...', '项目开发最新动态', '22222222-2222-2222-2222-222222222222', '新闻资讯', '新闻,项目,动态', 'published', '00000000-0000-0000-0000-000000000000', '管理员', 234, 45, 12, NOW() - INTERVAL '2 days', true, false, 90, NOW() - INTERVAL '7 days', NOW() - INTERVAL '2 days', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000')
ON CONFLICT ("Id") DO NOTHING;
"@
    $command.ExecuteNonQuery()
    
    Write-Host "文章表创建完成！" -ForegroundColor Green
    
    # 验证表是否创建成功
    Write-Host "验证表结构..." -ForegroundColor Yellow
    $command.CommandText = "SELECT COUNT(*) FROM gs_articles;"
    $articleCount = $command.ExecuteScalar()
    Write-Host "文章表中有 $articleCount 条记录" -ForegroundColor Green
    
    $command.CommandText = "SELECT COUNT(*) FROM gs_article_categories;"
    $categoryCount = $command.ExecuteScalar()
    Write-Host "文章分类表中有 $categoryCount 条记录" -ForegroundColor Green
    
} catch {
    Write-Host "创建表时发生错误: $($_.Exception.Message)" -ForegroundColor Red
} finally {
    if ($connection -and $connection.State -eq 'Open') {
        $connection.Close()
    }
} 
param(
    [string]$ConnectionString = "Host=localhost;Database=GroupSixDb;Username=postgres;Password=123456"
)

Write-Host "开始创建文章表..." -ForegroundColor Green

# 添加Npgsql程序集
Add-Type -Path "C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.0\System.Data.dll"
Add-Type -Path "C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.0\System.Data.Common.dll"

try {
    # 创建连接
    $connection = New-Object System.Data.Odbc.OdbcConnection
    $connection.ConnectionString = "Driver={PostgreSQL UNICODE};Server=localhost;Database=GroupSixDb;Uid=postgres;Pwd=123456;"
    
    $connection.Open()
    Write-Host "数据库连接成功！" -ForegroundColor Green
    
    # 创建命令对象
    $command = $connection.CreateCommand()
    
    # 创建文章分类表
    Write-Host "创建文章分类表..." -ForegroundColor Yellow
    $command.CommandText = @"
CREATE TABLE IF NOT EXISTS gs_article_categories (
    "Id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    "Name" character varying(100) NOT NULL,
    "Description" character varying(500) DEFAULT '',
    "ParentId" uuid,
    "Level" integer NOT NULL DEFAULT 1,
    "SortOrder" integer NOT NULL DEFAULT 0,
    "Code" text,
    "IsActive" boolean NOT NULL DEFAULT true,
    "IsDeleted" boolean NOT NULL DEFAULT false,
    "CreatedAt" timestamp with time zone NOT NULL DEFAULT NOW(),
    "UpdatedAt" timestamp with time zone NOT NULL DEFAULT NOW(),
    "CreatedBy" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',
    "UpdatedBy" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',
    "DisplayOrder" integer NOT NULL DEFAULT 0,
    "Remarks" text
);
"@
    $command.ExecuteNonQuery()
    
    # 创建文章表
    Write-Host "创建文章表..." -ForegroundColor Yellow
    $command.CommandText = @"
CREATE TABLE IF NOT EXISTS gs_articles (
    "Id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    "Title" character varying(200) NOT NULL,
    "Content" text NOT NULL,
    "Summary" character varying(500) DEFAULT '',
    "CategoryId" uuid NOT NULL,
    "CategoryName" character varying(100) DEFAULT '',
    "Tags" character varying(500) DEFAULT '',
    "Status" character varying(20) NOT NULL DEFAULT 'draft',
    "AuthorId" uuid NOT NULL,
    "AuthorName" character varying(50) DEFAULT '',
    "CoverImage" character varying(500) DEFAULT '',
    "ViewCount" integer NOT NULL DEFAULT 0,
    "LikeCount" integer NOT NULL DEFAULT 0,
    "CommentCount" integer NOT NULL DEFAULT 0,
    "PublishedAt" timestamp with time zone,
    "SeoTitle" character varying(200) DEFAULT '',
    "SeoKeywords" character varying(500) DEFAULT '',
    "SeoDescription" character varying(1000) DEFAULT '',
    "IsTop" boolean NOT NULL DEFAULT false,
    "IsRecommend" boolean NOT NULL DEFAULT false,
    "SortWeight" integer NOT NULL DEFAULT 0,
    "Code" text,
    "IsActive" boolean NOT NULL DEFAULT true,
    "IsDeleted" boolean NOT NULL DEFAULT false,
    "CreatedAt" timestamp with time zone NOT NULL DEFAULT NOW(),
    "UpdatedAt" timestamp with time zone NOT NULL DEFAULT NOW(),
    "CreatedBy" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',
    "UpdatedBy" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',
    "DisplayOrder" integer NOT NULL DEFAULT 0,
    "Remarks" text
);
"@
    $command.ExecuteNonQuery()
    
    # 创建索引
    Write-Host "创建索引..." -ForegroundColor Yellow
    $indexes = @(
        "CREATE INDEX IF NOT EXISTS `"IX_gs_article_categories_ParentId`" ON gs_article_categories (`"ParentId`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_article_categories_Level`" ON gs_article_categories (`"Level`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_article_categories_SortOrder`" ON gs_article_categories (`"SortOrder`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_CategoryId`" ON gs_articles (`"CategoryId`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_AuthorId`" ON gs_articles (`"AuthorId`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_Status`" ON gs_articles (`"Status`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_PublishedAt`" ON gs_articles (`"PublishedAt`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_IsTop`" ON gs_articles (`"IsTop`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_IsRecommend`" ON gs_articles (`"IsRecommend`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_SortWeight`" ON gs_articles (`"SortWeight`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_CreatedAt`" ON gs_articles (`"CreatedAt`");"
    )
    
    foreach ($index in $indexes) {
        $command.CommandText = $index
        $command.ExecuteNonQuery()
    }
    
    # 插入测试数据
    Write-Host "插入测试数据..." -ForegroundColor Yellow
    
    # 插入文章分类
    $command.CommandText = @"
INSERT INTO gs_article_categories ("Id", "Name", "Description", "Level", "SortOrder", "CreatedAt", "UpdatedAt", "CreatedBy", "UpdatedBy") VALUES
('11111111-1111-1111-1111-111111111111', '技术文章', '技术相关的文章分类', 1, 1, NOW(), NOW(), '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('22222222-2222-2222-2222-222222222222', '新闻资讯', '新闻资讯类文章', 1, 2, NOW(), NOW(), '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('33333333-3333-3333-3333-333333333333', '教程指南', '教程和指南类文章', 1, 3, NOW(), NOW(), '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000')
ON CONFLICT ("Id") DO NOTHING;
"@
    $command.ExecuteNonQuery()
    
    # 插入文章
    $command.CommandText = @"
INSERT INTO gs_articles ("Id", "Title", "Content", "Summary", "CategoryId", "CategoryName", "Tags", "Status", "AuthorId", "AuthorName", "ViewCount", "LikeCount", "CommentCount", "PublishedAt", "IsTop", "IsRecommend", "SortWeight", "CreatedAt", "UpdatedAt", "CreatedBy", "UpdatedBy") VALUES
('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'ASP.NET Core 开发指南', '这是一篇关于ASP.NET Core开发的详细指南...', 'ASP.NET Core开发入门指南', '11111111-1111-1111-1111-111111111111', '技术文章', 'ASP.NET Core,C#,Web开发', 'published', '00000000-0000-0000-0000-000000000000', '管理员', 150, 25, 8, NOW() - INTERVAL '5 days', true, true, 100, NOW() - INTERVAL '10 days', NOW() - INTERVAL '5 days', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'Vue.js 前端开发实践', 'Vue.js是现代前端开发的重要框架...', 'Vue.js前端开发最佳实践', '11111111-1111-1111-1111-111111111111', '技术文章', 'Vue.js,JavaScript,前端', 'published', '00000000-0000-0000-0000-000000000000', '管理员', 89, 15, 3, NOW() - INTERVAL '3 days', false, true, 80, NOW() - INTERVAL '8 days', NOW() - INTERVAL '3 days', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('cccccccc-cccc-cccc-cccc-cccccccccccc', '数据库设计原则', '良好的数据库设计是系统成功的关键...', '数据库设计的基本原则和最佳实践', '33333333-3333-3333-3333-333333333333', '教程指南', '数据库,设计,SQL', 'published', '00000000-0000-0000-0000-000000000000', '管理员', 67, 12, 5, NOW() - INTERVAL '1 day', false, false, 60, NOW() - INTERVAL '6 days', NOW() - INTERVAL '1 day', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('dddddddd-dddd-dddd-dddd-dddddddddddd', '系统架构设计', '系统架构设计需要考虑多个方面...', '系统架构设计的方法和技巧', '33333333-3333-3333-3333-333333333333', '教程指南', '架构,设计,系统', 'draft', '00000000-0000-0000-0000-000000000000', '管理员', 0, 0, 0, NULL, false, false, 0, NOW(), NOW(), '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee', '项目开发新闻', '最新的项目开发动态和进展...', '项目开发最新动态', '22222222-2222-2222-2222-222222222222', '新闻资讯', '新闻,项目,动态', 'published', '00000000-0000-0000-0000-000000000000', '管理员', 234, 45, 12, NOW() - INTERVAL '2 days', true, false, 90, NOW() - INTERVAL '7 days', NOW() - INTERVAL '2 days', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000')
ON CONFLICT ("Id") DO NOTHING;
"@
    $command.ExecuteNonQuery()
    
    Write-Host "文章表创建完成！" -ForegroundColor Green
    
    # 验证表是否创建成功
    Write-Host "验证表结构..." -ForegroundColor Yellow
    $command.CommandText = "SELECT COUNT(*) FROM gs_articles;"
    $articleCount = $command.ExecuteScalar()
    Write-Host "文章表中有 $articleCount 条记录" -ForegroundColor Green
    
    $command.CommandText = "SELECT COUNT(*) FROM gs_article_categories;"
    $categoryCount = $command.ExecuteScalar()
    Write-Host "文章分类表中有 $categoryCount 条记录" -ForegroundColor Green
    
} catch {
    Write-Host "创建表时发生错误: $($_.Exception.Message)" -ForegroundColor Red
} finally {
    if ($connection -and $connection.State -eq 'Open') {
        $connection.Close()
    }
} 
param(
    [string]$ConnectionString = "Host=localhost;Database=GroupSixDb;Username=postgres;Password=123456"
)

Write-Host "开始创建文章表..." -ForegroundColor Green

# 添加Npgsql程序集
Add-Type -Path "C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.0\System.Data.dll"
Add-Type -Path "C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.0\System.Data.Common.dll"

try {
    # 创建连接
    $connection = New-Object System.Data.Odbc.OdbcConnection
    $connection.ConnectionString = "Driver={PostgreSQL UNICODE};Server=localhost;Database=GroupSixDb;Uid=postgres;Pwd=123456;"
    
    $connection.Open()
    Write-Host "数据库连接成功！" -ForegroundColor Green
    
    # 创建命令对象
    $command = $connection.CreateCommand()
    
    # 创建文章分类表
    Write-Host "创建文章分类表..." -ForegroundColor Yellow
    $command.CommandText = @"
CREATE TABLE IF NOT EXISTS gs_article_categories (
    "Id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    "Name" character varying(100) NOT NULL,
    "Description" character varying(500) DEFAULT '',
    "ParentId" uuid,
    "Level" integer NOT NULL DEFAULT 1,
    "SortOrder" integer NOT NULL DEFAULT 0,
    "Code" text,
    "IsActive" boolean NOT NULL DEFAULT true,
    "IsDeleted" boolean NOT NULL DEFAULT false,
    "CreatedAt" timestamp with time zone NOT NULL DEFAULT NOW(),
    "UpdatedAt" timestamp with time zone NOT NULL DEFAULT NOW(),
    "CreatedBy" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',
    "UpdatedBy" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',
    "DisplayOrder" integer NOT NULL DEFAULT 0,
    "Remarks" text
);
"@
    $command.ExecuteNonQuery()
    
    # 创建文章表
    Write-Host "创建文章表..." -ForegroundColor Yellow
    $command.CommandText = @"
CREATE TABLE IF NOT EXISTS gs_articles (
    "Id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    "Title" character varying(200) NOT NULL,
    "Content" text NOT NULL,
    "Summary" character varying(500) DEFAULT '',
    "CategoryId" uuid NOT NULL,
    "CategoryName" character varying(100) DEFAULT '',
    "Tags" character varying(500) DEFAULT '',
    "Status" character varying(20) NOT NULL DEFAULT 'draft',
    "AuthorId" uuid NOT NULL,
    "AuthorName" character varying(50) DEFAULT '',
    "CoverImage" character varying(500) DEFAULT '',
    "ViewCount" integer NOT NULL DEFAULT 0,
    "LikeCount" integer NOT NULL DEFAULT 0,
    "CommentCount" integer NOT NULL DEFAULT 0,
    "PublishedAt" timestamp with time zone,
    "SeoTitle" character varying(200) DEFAULT '',
    "SeoKeywords" character varying(500) DEFAULT '',
    "SeoDescription" character varying(1000) DEFAULT '',
    "IsTop" boolean NOT NULL DEFAULT false,
    "IsRecommend" boolean NOT NULL DEFAULT false,
    "SortWeight" integer NOT NULL DEFAULT 0,
    "Code" text,
    "IsActive" boolean NOT NULL DEFAULT true,
    "IsDeleted" boolean NOT NULL DEFAULT false,
    "CreatedAt" timestamp with time zone NOT NULL DEFAULT NOW(),
    "UpdatedAt" timestamp with time zone NOT NULL DEFAULT NOW(),
    "CreatedBy" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',
    "UpdatedBy" uuid NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',
    "DisplayOrder" integer NOT NULL DEFAULT 0,
    "Remarks" text
);
"@
    $command.ExecuteNonQuery()
    
    # 创建索引
    Write-Host "创建索引..." -ForegroundColor Yellow
    $indexes = @(
        "CREATE INDEX IF NOT EXISTS `"IX_gs_article_categories_ParentId`" ON gs_article_categories (`"ParentId`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_article_categories_Level`" ON gs_article_categories (`"Level`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_article_categories_SortOrder`" ON gs_article_categories (`"SortOrder`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_CategoryId`" ON gs_articles (`"CategoryId`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_AuthorId`" ON gs_articles (`"AuthorId`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_Status`" ON gs_articles (`"Status`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_PublishedAt`" ON gs_articles (`"PublishedAt`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_IsTop`" ON gs_articles (`"IsTop`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_IsRecommend`" ON gs_articles (`"IsRecommend`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_SortWeight`" ON gs_articles (`"SortWeight`");",
        "CREATE INDEX IF NOT EXISTS `"IX_gs_articles_CreatedAt`" ON gs_articles (`"CreatedAt`");"
    )
    
    foreach ($index in $indexes) {
        $command.CommandText = $index
        $command.ExecuteNonQuery()
    }
    
    # 插入测试数据
    Write-Host "插入测试数据..." -ForegroundColor Yellow
    
    # 插入文章分类
    $command.CommandText = @"
INSERT INTO gs_article_categories ("Id", "Name", "Description", "Level", "SortOrder", "CreatedAt", "UpdatedAt", "CreatedBy", "UpdatedBy") VALUES
('11111111-1111-1111-1111-111111111111', '技术文章', '技术相关的文章分类', 1, 1, NOW(), NOW(), '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('22222222-2222-2222-2222-222222222222', '新闻资讯', '新闻资讯类文章', 1, 2, NOW(), NOW(), '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('33333333-3333-3333-3333-333333333333', '教程指南', '教程和指南类文章', 1, 3, NOW(), NOW(), '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000')
ON CONFLICT ("Id") DO NOTHING;
"@
    $command.ExecuteNonQuery()
    
    # 插入文章
    $command.CommandText = @"
INSERT INTO gs_articles ("Id", "Title", "Content", "Summary", "CategoryId", "CategoryName", "Tags", "Status", "AuthorId", "AuthorName", "ViewCount", "LikeCount", "CommentCount", "PublishedAt", "IsTop", "IsRecommend", "SortWeight", "CreatedAt", "UpdatedAt", "CreatedBy", "UpdatedBy") VALUES
('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'ASP.NET Core 开发指南', '这是一篇关于ASP.NET Core开发的详细指南...', 'ASP.NET Core开发入门指南', '11111111-1111-1111-1111-111111111111', '技术文章', 'ASP.NET Core,C#,Web开发', 'published', '00000000-0000-0000-0000-000000000000', '管理员', 150, 25, 8, NOW() - INTERVAL '5 days', true, true, 100, NOW() - INTERVAL '10 days', NOW() - INTERVAL '5 days', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'Vue.js 前端开发实践', 'Vue.js是现代前端开发的重要框架...', 'Vue.js前端开发最佳实践', '11111111-1111-1111-1111-111111111111', '技术文章', 'Vue.js,JavaScript,前端', 'published', '00000000-0000-0000-0000-000000000000', '管理员', 89, 15, 3, NOW() - INTERVAL '3 days', false, true, 80, NOW() - INTERVAL '8 days', NOW() - INTERVAL '3 days', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('cccccccc-cccc-cccc-cccc-cccccccccccc', '数据库设计原则', '良好的数据库设计是系统成功的关键...', '数据库设计的基本原则和最佳实践', '33333333-3333-3333-3333-333333333333', '教程指南', '数据库,设计,SQL', 'published', '00000000-0000-0000-0000-000000000000', '管理员', 67, 12, 5, NOW() - INTERVAL '1 day', false, false, 60, NOW() - INTERVAL '6 days', NOW() - INTERVAL '1 day', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('dddddddd-dddd-dddd-dddd-dddddddddddd', '系统架构设计', '系统架构设计需要考虑多个方面...', '系统架构设计的方法和技巧', '33333333-3333-3333-3333-333333333333', '教程指南', '架构,设计,系统', 'draft', '00000000-0000-0000-0000-000000000000', '管理员', 0, 0, 0, NULL, false, false, 0, NOW(), NOW(), '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'),
('eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee', '项目开发新闻', '最新的项目开发动态和进展...', '项目开发最新动态', '22222222-2222-2222-2222-222222222222', '新闻资讯', '新闻,项目,动态', 'published', '00000000-0000-0000-0000-000000000000', '管理员', 234, 45, 12, NOW() - INTERVAL '2 days', true, false, 90, NOW() - INTERVAL '7 days', NOW() - INTERVAL '2 days', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000')
ON CONFLICT ("Id") DO NOTHING;
"@
    $command.ExecuteNonQuery()
    
    Write-Host "文章表创建完成！" -ForegroundColor Green
    
    # 验证表是否创建成功
    Write-Host "验证表结构..." -ForegroundColor Yellow
    $command.CommandText = "SELECT COUNT(*) FROM gs_articles;"
    $articleCount = $command.ExecuteScalar()
    Write-Host "文章表中有 $articleCount 条记录" -ForegroundColor Green
    
    $command.CommandText = "SELECT COUNT(*) FROM gs_article_categories;"
    $categoryCount = $command.ExecuteScalar()
    Write-Host "文章分类表中有 $categoryCount 条记录" -ForegroundColor Green
    
} catch {
    Write-Host "创建表时发生错误: $($_.Exception.Message)" -ForegroundColor Red
} finally {
    if ($connection -and $connection.State -eq 'Open') {
        $connection.Close()
    }
} 